//
//  CDBottomView.m
//  CuriosityDaily3
//
//  Created by 安林 on 16/9/23.
//  Copyright © 2016年 theBestTeam. All rights reserved.
//

#import "CDBottomView.h"

#import "CDButtomCell.h"
#import "CDButtomBtnType.h"

//注册id
static NSString* cellid = @"cellid";
@interface CDBottomView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak)UITableView *categoryView;
@end

@implementation CDBottomView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}


- (void)awakeFromNib
{
    [self setupUI];
    
}
- (void)setupUI
{  self.buttomTypeData = [self loadBusinessTypeData];
    UITableView *tableView = [[UITableView alloc]init];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 
    [tableView registerClass:[CDButtomCell class] forCellReuseIdentifier:cellid];
    [self addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.rowHeight = 60;
    // 设置自动布局
    [tableView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.left.right.offset(0);
        make.bottom.offset(-80);
    }];
    
  
  
}
#pragma mark 数据源和代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.buttomTypeData.count;
}
#pragma mark 代理
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CDButtomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid forIndexPath:indexPath];
    cell.ButtomBtnType = self.buttomTypeData[indexPath.row];
    if (indexPath.row ==1) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.frame = CGRectMake(127, 20, 15, 15);
        imageView.image = [UIImage imageNamed:@"TableViewArrow@2x.png"];
        [cell addSubview:imageView];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}
- (NSArray*)loadBusinessTypeData
{
    // 1. 加载 plist
    NSURL* url = [[NSBundle mainBundle] URLForResource:@"ButtonType.plist" withExtension:nil];
    NSArray* list = [NSArray arrayWithContentsOfURL:url];
    
    // 2. 遍历数组，字典转模型
    NSMutableArray* arrayM = [NSMutableArray array];
    for (NSDictionary* dict in list) {
        [arrayM addObject:[CDButtomBtnType businessTypeWithDict:dict]];
    }
    
    // 3. 返回数组
    return arrayM.copy;
}
#pragma mark 新闻分类点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 1) {
        

        [[NSNotificationCenter defaultCenter]postNotificationName:@"cagegory" object:nil];
    }
}
@end